package one.stand.dto;

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import one.stand.model.ResultModel;
import one.stand.util.OneListUtil;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author cjr
 */
public class ConvertUtils {
    public static <T, R> R result(ResultModel<T> resultModel, Function<T, R> handler) {
        return Objects.nonNull(resultModel) && resultModel.isSuccess() ? handler.apply(resultModel.getData()) : null;
    }

    public static <T, R> List<R> list(List<T> olist, Function<T, R> handler) {
        if (OneListUtil.isNotEmpty(olist)) {
            return olist.stream().map(handler).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    public static <T, R> List<R> listWithoutNull(List<T> olist, Function<T, R> handler) {
        return list(olist, handler, Objects::nonNull);
    }

    public static <T, R> List<R> list(List<T> olist, Function<T, R> handler, Predicate<R> predicate) {
        if (OneListUtil.isNotEmpty(olist)) {
            return olist.stream().map(handler).filter(predicate).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    public static <T, R> PageResponse<R> page(List<T> olist, Function<T, R> handler) {
        PageResponse<R> pagelist = new PageResponse<>();
        if (Objects.nonNull(olist) && olist instanceof Page) {
            pagelist.setTotalPage(((Page) olist).getPages());
            pagelist.setCount(((Page) olist).getTotal());
            List<R> list = list(((Page<T>) olist).getResult(), handler);
            pagelist.setResult(list);
        } else {
            pagelist.setTotalPage(0);
            pagelist.setCount(0L);
        }
        return pagelist;
    }

    public static <T, R> PageResponse2<R> page2(List<T> olist, Function<T, R> handler) {
        PageResponse2<R> pagelist = new PageResponse2<>();
        if (Objects.nonNull(olist) && olist instanceof Page) {
            pagelist.setPages(((Page) olist).getPages());
            pagelist.setTotal(((Page) olist).getTotal());
            List<R> list = list(((Page<T>) olist).getResult(), handler, Objects::nonNull);
            pagelist.setList(list);
        } else if (OneListUtil.isNotEmpty(olist)) {
            pagelist.setPages(1);
            pagelist.setTotal((long) olist.size());
            List<R> list = list(olist, handler, Objects::nonNull);
            pagelist.setList(list);
        } else {
            pagelist.setPages(0);
            pagelist.setTotal(0L);
        }
        return pagelist;
    }

    public static <T, R> PageResponse2<R> page2(PageResponse2<T> olist, Function<T, R> handler) {
        PageResponse2<R> pagelist = new PageResponse2<>();
        if (Objects.nonNull(olist)) {
            pagelist.setPages(olist.getPages());
            pagelist.setTotal(olist.getTotal());
            List<R> list = list(olist.getList(), handler, Objects::nonNull);
            pagelist.setList(list);
        } else {
            pagelist.setPages(0);
            pagelist.setTotal(0L);
        }
        return pagelist;
    }
}
